﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Convertor.Interface;
using Wpf.CommonTools;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.DispatchTemplate
{

    [Serializable]
    public class ConfigContent : IConfigContent, ISerializable, ICloneable, INotifyPropertyChanged, IPluginTemplateList
    {
        private ITemplateToLoadViewModel tempModel;

        public ITemplateToLoadViewModel TempModel
        {
            get { return AllTemplate == null ? null : AllTemplate.FirstOrDefault(p => p.UniqueId == uniqueId); }
            set
            {
                tempModel = value;
                if (value != null)
                {
                    uniqueId = value.UniqueId;
                    templateName = value.Name;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("UniqueId"));
                        PropertyChanged(this, new PropertyChangedEventArgs("TemplateName"));
                        PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                    }
                }
            }
        }
        private string uniqueId;

        public string UniqueId
        {
            get { return uniqueId; }
            set
            {
                uniqueId = value; if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("UniqueId"));
                    PropertyChanged(this, new PropertyChangedEventArgs("TemplateName"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private string templateName;

        public string TemplateName
        {
            get { return templateName; }
            set { templateName = value; }
        }

        public ConfigContent()
        {
        }

        public override string ToString()
        {
            return ToolTip;
        }

        #region ISerializable 成员

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("TemplateName", TemplateName, typeof(string));
            info.AddValue("UniqueId", UniqueId, typeof(string));
        }

        public ConfigContent(SerializationInfo info, StreamingContext context)
        {
            try
            {
                TemplateName = (string)info.GetValue("TemplateName", typeof(string));
            }
            catch { }
            try
            {
                UniqueId = (string)info.GetValue("UniqueId", typeof(string));
            }
            catch { }
        }

        #endregion

        #region IToolTip 成员

        public string ToolTip
        {
            get { return string.Format(UILanguage.FindResource("Plugin_DispatchTemplate_Info"), this.templateName); }
        }

        #endregion

        #region ICloneable 成员

        public object Clone()
        {
            ConfigContent content = new ConfigContent()
            {
                TemplateName = this.TemplateName,
                UniqueId = this.UniqueId,
            };
            return content;
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IPluginTemplateList 成员

        public System.Collections.ObjectModel.ObservableCollection<ITemplateToLoadViewModel> AllTemplate { get; set; }

        #endregion

    }
}
